﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Atomic.User;
using Atomic.User.Entities;
using Atomic.Common.Validators;
using Newtonsoft.Json;
using Atomic.Db;
namespace Atomic.Web.Handlers.User
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : BaseHandler
    {
        static Dictionary<string, Action<HttpContext>> actions = new Dictionary<string, Action<HttpContext>>();
        protected override Dictionary<string, Action<HttpContext>> Actions
        {
            get { return actions; }
        }

        /// <summary>
        /// 添加新的用户类型
        /// </summary>
        /// <param name="context"></param>
        public void AddOrUpdateUserType(HttpContext context)
        {
            IUser user = ContextFactory.Context.GetObject("User") as IUser;
            UserTypeEntity entity = new UserTypeEntity()
            {
                ID = Convert.ToInt32(context.Request["id"]),
                Name = context.Request["name"]
            };
            if ((VResult = entity.Validate()).Success)
            {
                if (entity.ID == 0)
                {
                    if (!user.AddType(entity))   //新建用户类型
                    {
                        context.Response.Write(JsonConvert.SerializeObject(new
                        {
                            Success = false,
                            Message = user.GetError().Message
                        }));
                        return;
                    }
                }
                else
                {
                    if (!user.UpdateType(entity, entity.ID))
                    {
                        context.Response.Write(JsonConvert.SerializeObject(new
                        {
                            Success = false,
                            Message = user.GetError().Message
                        }));
                        return;
                    }
                }
                //成功
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true,
                    Message = string.Empty
                }));
                return;
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = VResult.FailureMessage
                }));
                return;
            }
        }

        /// <summary>
        /// 获取所有用户类型列表
        /// </summary>
        /// <param name="context"></param>
        public void GetUserTypes(HttpContext context)
        {
            IUser user = ContextFactory.Context.GetObject("User") as IUser;
            context.Response.Write(JsonConvert.SerializeObject(new
            {
                total = 0,
                rows = user.GetTypes()
            }));
        }

        /// <summary>
        /// 删除用户类型
        /// </summary>
        /// <param name="context"></param>
        public void RemoveUserType(HttpContext context)
        {
            IUser user = ContextFactory.Context.GetObject("User") as IUser;
            if (!user.RemoveType(Convert.ToInt32(context.Request["id"])))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = user.GetError().Message
                }));
                return;
            }
            context.Response.Write(JsonConvert.SerializeObject(new
            {
                Success = true
            }));
        }

        #region 用户类型字段
        public void GetUserExtendFields(HttpContext context)
        {
            IUser user = ContextFactory.Context.GetObject("User") as IUser;
            context.Response.Write(JsonConvert.SerializeObject(new
            {
                total = 0,
                rows = user.GetExtendPropertiesByUserType(Convert.ToInt32(context.Request["UserTypeID"]))
            }));
        }
        public void AddUserExtendField(HttpContext context)
        {
            IUser user = ContextFactory.Context.GetObject("User") as IUser;
            UserExtendFieldEntity entity = new UserExtendFieldEntity()
            {
                UserTypeID = Convert.ToInt32(context.Request["UserTypeID"]),
                Name = context.Request["Name"],
                DisplayName = context.Request["DisplayName"],
                Comment = context.Request["Comment"],
                FieldType = context.Request["FieldType"],
                Length = Convert.ToInt32(context.Request["Length"])
            };
            if (!(VResult=entity.Validate()).Success)
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = VResult.FailureMessage
                }));
                return;
            }
            if (user.AddProperty(entity))
            {
                //成功
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true,
                    Message = string.Empty
                }));
                return;
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = user.GetError().Message
                }));
                return;
            }
        }
        #endregion
    }
}